AddCSLuaFile( "shared.lua" )

SWEP.HoldType = "grenade"

if ( CLIENT ) then

SWEP.PrintName = "Frag Grenade"
SWEP.Author = "S1L3NT"
SWEP.Slot = 3
SWEP.SlotPos = 1
SWEP.IconLetter = "g"

killicon.AddFont( "weapon_mdm_frag_grenade", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )

end



SWEP.Base = "weapon_mdm_base"

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.ViewModel = "models/weapons/v_eq_fraggrenade.mdl"
SWEP.WorldModel = "models/weapons/w_eq_fraggrenade.mdl"

SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false


SWEP.Primary.Sound = Sound("Weapon_FRAG.Single")
SWEP.Primary.Recoil = 0.15
SWEP.Primary.Damage = 20
SWEP.Primary.NumShots = 1
SWEP.Primary.Cone = 0.03
SWEP.Primary.ClipSize = 1
SWEP.Primary.Delay = 2
SWEP.Primary.DefaultClip = 2
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "grenade"

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"


function SWEP:Deploy()
	if self.Weapon:Clip1() >= 1 then
		self.Weapon:SendWeaponAnim( ACT_VM_PULLPIN )
		self.Weapon:SetNextPrimaryFire(CurTime()+0.25)
		return true
	elseif self.Owner:GetAmmoCount(self.Weapon:GetPrimaryAmmoType()) >= 1 then
		self.Weapon:Reload()
	else
		if SERVER then
			self.Owner:SelectWeapon(self.Owner.Weapons[string.upper(string.sub(self.Owner:GetNWString("class"),1,1))..string.sub(self.Owner:GetNWString("class"),2,100)][1])
		end
		return false
	end
end

function SWEP:Reload()
	if !timer.IsTimer("pin"..self.Weapon:EntIndex()) and self.Weapon:Clip1() < 1 then
		timer.Remove("fire"..self.Weapon:EntIndex())
		self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
		timer.Create("pin"..self.Weapon:EntIndex(),0.6,1,function()
			self.Weapon:SendWeaponAnim( ACT_VM_PULLPIN )
			timer.Remove("pin"..self.Weapon:EntIndex())
		end)
		self.Weapon:SetNextPrimaryFire(CurTime() + 1)
		self.Weapon:SetClip1(1)
		self.Owner:RemoveAmmo( 1, self.Primary.Ammo, false )
	end
end

function SWEP:PrimaryAttack()
	if self.Weapon:Clip1() >= 1 and !timer.IsTimer("fire"..self.Weapon:EntIndex()) then
		timer.Create("fire"..self.Weapon:EntIndex(),0.25,1,function()
			if self.Weapon then
				self.Weapon:SetClip1(self.Weapon:Clip1()-1)
				self.Weapon:SendWeaponAnim( ACT_VM_THROW )
				local frag = ents.Create("mdm_sticky")
				if frag:IsValid() then
					frag:Spawn()
					frag.Owner = self.Owner
					frag:GetPhysicsObject():SetPos(self.Owner:GetShootPos()+self.Owner:GetAimVector()*30)
					frag:GetPhysicsObject():SetVelocity(self.Owner:GetAimVector()*1000+self.Owner:GetVelocity())
					frag:GetPhysicsObject():SetAngle(Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180)))
					frag.Timer = 2.5
				end
				timer.Remove("fire"..self.Weapon:EntIndex())
			else
				timer.Remove("fire"..self.Weapon:EntIndex())
			end
		end)
	end
end

function SWEP:Think()
	
end